package cn.lysj.framework.common.controller;

import cn.lysj.framework.common.CommonConstant;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.repository.UserRepository;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.common.validate.annotation.Validator;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.security.auth.Subject;

import javax.validation.Valid;

/**
 * @author zk
 * @description 用户登录
 * @create 2018-07-29 22:07
 **/
@RestController
public class HomeController {

    @Resource
    private UserRepository userRepository;

//    @PostMapping("/login")
//    public Resp login(User user) {
//        return null;
//    }

    @PostMapping("/register")
    public Resp register(@Validator User user) {
        String password = new BCryptPasswordEncoder().encode(user.getPassword());
        user.setPassword(password);
        userRepository.saveAndFlush(user);
        return new Resp("注册成功");
    }

    @GetMapping("/unauth")
    public Resp unauth() {
        return new Resp(Resp.NO_LOGIN, "您尚未登录");
    }

    @GetMapping("/unauthorized")
    public Resp unauthorized() {
        return new Resp(Resp.NO_ACCESS, "您尚未拥有该权限");
    }

    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Resp logout() {
        return new Resp("注销成功");
    }
}
